/**
 * ReportGear(2011)
 */
package com.reportgear.report.model.cell.value.salary.enhance;

import java.awt.BorderLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.reportgear.core.data.SalaryHelper;
import com.reportgear.core.data.model.SalaryDataObject;
import com.reportgear.core.swing.pane.base.JBasePane;
import com.reportgear.report.context.ReportContext;
import com.reportgear.report.context.salary.SalaryObjectGroupInfoPane;
import com.reportgear.report.core.api.idname.DefaultIdName;

/**
 * 预览时参数设置
 * 
 * @version 1.0 2011-6-15
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class PreviewParametersSetterPane extends JBasePane {
	private SalaryObjectGroupInfoPane paramPane;
	private JComboBox paymentListCbx;

	public PreviewParametersSetterPane() {
		this.setBorder(BorderFactory.createEtchedBorder());
		this.setLayout(new BorderLayout());

		paramPane = new SalaryObjectGroupInfoPane();
		this.add(paramPane, BorderLayout.CENTER);

		JPanel pane = new JPanel();
		pane.setLayout(new BorderLayout());
		pane.add(new JLabel("发放表设置"), BorderLayout.WEST);

		paymentListCbx = new JComboBox();
		List<SalaryDataObject> paymentList = SalaryHelper.INSTANCE.getLastPayments();
		int paymentId = ReportContext.INSTANCE.getPaymentId();
		Object selected = null;

		for (SalaryDataObject sdo : paymentList) {
			int id = sdo.getInt("id");
			Object item = new DefaultIdName<Integer>(id, sdo.getString("name"));
			if (paymentId == id) {
				selected = item;
			}
			paymentListCbx.addItem(item);
		}

		paymentListCbx.setSelectedItem(selected);
		pane.add(paymentListCbx, BorderLayout.CENTER);

		this.add(pane, BorderLayout.SOUTH);
	}

	@SuppressWarnings("unchecked")
	public void updateValues() {
		ReportContext ctx = ReportContext.INSTANCE;
		Object select = paymentListCbx.getSelectedItem();
		if (select != null) {
			ctx.setPaymentId(((DefaultIdName<Integer>) select).getId());
		}
		ctx.setShowLevelList(this.paramPane.getShowLevel());
		ctx.setGroupLevel(this.paramPane.getGroupLevel());
		ctx.setSalaryObjectType(this.paramPane.getSalaryObjectType());
	}
}
